package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.pojo.SysMailLog;
import com.cl.short_video_java.service.SysMailLogService;
import com.cl.short_video_java.mapper.SysMailLogMapper;
import com.cl.short_video_java.util.ExceUtil;
import com.cl.short_video_java.vo.sys.MailLogExceSearch;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author object
* @description 针对表【sys_mail_log】的数据库操作Service实现
* @createDate 2024-11-02 11:39:53
*/
@Service
public class SysMailLogServiceImpl extends ServiceImpl<SysMailLogMapper, SysMailLog>
    implements SysMailLogService{

	@Override
	public void addLog(SysMailLog sysMailLog) {

	}

    @Override
    public ResponseEntity<byte[]> getExceService(MailLogExceSearch mailLogExceSearch) throws IOException {
        List<SysMailLog> list=new ArrayList<>();
        if (mailLogExceSearch.getIdlist().length()>0)
        {
            String[] split = mailLogExceSearch.getIdlist().split(",");
            list = this.listByIds(Arrays.stream(split).toList());
        }else {
            list=this.list(new QueryWrapper<SysMailLog>().select("id,subject,content,create_time as time,user_id,send_mail,reception_mail,phone")
                    .eq(mailLogExceSearch.getId()!=null,"id",mailLogExceSearch.getId())
                    .eq(!mailLogExceSearch.getSubject().equals(""),"subject",mailLogExceSearch.getSubject())
                    .eq(mailLogExceSearch.getUserId()!=null,"user_id",mailLogExceSearch.getUserId()));
        }
        for (SysMailLog sysMailLog : list) {
            System.out.println(sysMailLog.getTime());
        }
        ByteArrayOutputStream outputStream = ExceUtil.exportSysMailLogExcel(list);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("sysmaillog", "系统日志.xlsx");
        return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK);
    }

    @Override
    public Boolean clearService() {
        return this.remove(null);
    }

    @Override
    public Boolean DeleteService(String idlist) {
        String[] split = idlist.split(",");
        return this.removeByIds(Arrays.stream(split).toList());
    }

    @Override
    public PageInfo<SysMailLog> getPageMailLogService(MailLogExceSearch mailLogExceSearch) {
        if (mailLogExceSearch.getPageNum()==0 || mailLogExceSearch.getPageSize()==0) {
            mailLogExceSearch.setPageNum(1);
            mailLogExceSearch.setPageSize(20);
        }
        PageHelper.startPage(mailLogExceSearch.getPageNum(),mailLogExceSearch.getPageSize());
        List<SysMailLog> list = this.list(new QueryWrapper<SysMailLog>().select("id,subject,content,create_time as time,user_id,send_mail,reception_mail,phone")
                .eq(mailLogExceSearch.getId() != null, "id", mailLogExceSearch.getId())
                .eq(mailLogExceSearch.getUserId() != null, "user_id", mailLogExceSearch.getUserId())
                .like("subject", mailLogExceSearch.getSubject())
                .orderByDesc("id"));
        return new PageInfo<SysMailLog>(list);
    }
}




